﻿using AliIotAmqpWorker.Common;
using IServices;
using System;
using System.Collections.Generic;
using System.Text;
using YL.Core.Entity;

namespace AliIotAmqpWorker
{
    public class DeviceAlarmService
    {
        private readonly IMeter_ClientInfoServices clientInfoServices;
        private readonly IMeter_DeviceAlarmHistoryServices deviceAlarmHistoryServices;
        private readonly IMeter_DeviceAlarmDetailServices deviceAlarmDetailServices;
        private readonly IMeter_DeviceInfoServices deviceInfoServices;
        private readonly IMeter_DeviceAlarmDayServices deviceAlarmDayServices;
        private readonly IMeter_DeviceAlarmMonthServices deviceAlarmMonthServices;

        public DeviceAlarmService(IMeter_ClientInfoServices ClientInfoServices,
                                  IMeter_DeviceAlarmHistoryServices DeviceAlarmHistoryServices,
                                  IMeter_DeviceAlarmDetailServices DeviceAlarmDetailServices,
                                  IMeter_DeviceInfoServices DeviceInfoServices,
                                  IMeter_DeviceAlarmDayServices meter_DeviceAlarmDayServices,
                                  IMeter_DeviceAlarmMonthServices meter_DeviceAlarmMonthServices)
        {
            clientInfoServices = ClientInfoServices;
            deviceAlarmHistoryServices = DeviceAlarmHistoryServices;
            deviceAlarmDetailServices = DeviceAlarmDetailServices;
            deviceInfoServices = DeviceInfoServices;
            deviceAlarmDayServices = meter_DeviceAlarmDayServices;
            deviceAlarmMonthServices = meter_DeviceAlarmMonthServices;
        }

        /// <summary>
        /// 新增报警
        /// </summary>
        /// <param name="deviceData"></param>
        /// <param name="device"></param>
        /// <param name="alarmDetail"></param>
        /// <param name="alarmType"></param>
        public void AddAlarm(AliIotDeviceData<ECU1151Info> deviceData, DeviceInfo device, string alarmDetail, string alarmType)
        {
            if (device != null)
            {
                ClientInfo clientInfo = clientInfoServices.QueryableToEntity(x => x.ClientId == device.ClientId);
                DeviceAlarmHistory deviceAlarm = new DeviceAlarmHistory();
                deviceAlarm.AlarmDetail = alarmDetail;
                deviceAlarm.AlarmEquip = clientInfo == null ? string.Empty : clientInfo.ClientName;
                deviceAlarm.AlarmType = alarmType;
                deviceAlarm.StartTime = DateTime.Now;
                deviceAlarm.EndTime = GlobalSetting.DefaultEndTime;//给初始值
                deviceAlarm.ConfirmTime = GlobalSetting.DefaultEndTime;//给初始值
                deviceAlarm.DeviceId = device.DeviceId;
                long id = deviceAlarmHistoryServices.InsertBigIdentity(deviceAlarm);
                if (id > 0)
                {
                    AddAlarmDetail(device.DeviceId, (int)id, deviceData);
                    AddAlarmDay(device, clientInfo);//增加天数据，以及月数据
                }
            }
        }
        /// <summary>
        /// 更新报警结束时间
        /// </summary>
        /// <param name="deviceData"></param>
        /// <param name="device"></param>
        /// <param name="alarmDetail"></param>
        /// <param name="alarmType"></param>
        public bool UpdateAlarm(AliIotDeviceData<ECU1151Info> deviceData, DeviceInfo device, string alarmDetail, string alarmType)
        {
            if (device != null)
            {
                DeviceAlarmHistory deviceAlarm = deviceAlarmHistoryServices.QueryableToEntity(x => x.DeviceId == device.DeviceId && x.EndTime == GlobalSetting.DefaultEndTime && x.AlarmType == alarmType);
                if (deviceAlarm != null)
                {
                    deviceAlarm.EndTime = DateTime.Now;
                    deviceAlarm.ConfirmTime= DateTime.Now;
                    if(deviceAlarmHistoryServices.UpdateEntity(deviceAlarm))
                    {
                        UpdateAlarmDay(device, deviceAlarm);
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 增加报警天数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void AddAlarmDay(DeviceInfo device, ClientInfo clientInfo)
        {
            DeviceAlarmDay deviceAlarmDay = new DeviceAlarmDay();
           
            deviceAlarmDay =deviceAlarmDayServices.QueryableToEntity(x=>x.Date== DateTime.Now.Date && x.DeviceId== device.DeviceId);
            if(deviceAlarmDay ==null)
            {
                deviceAlarmDay = new DeviceAlarmDay();
                deviceAlarmDay.AlarmCount = 1;
                deviceAlarmDay.Date = DateTime.Now.Date;
                deviceAlarmDay.DeviceId = device.DeviceId;
                deviceAlarmDay.DeviceName = device.DeviceName;
                deviceAlarmDay.ClientName = clientInfo.ClientName;
                deviceAlarmDayServices.InsertBigIdentity(deviceAlarmDay);
            }
            else
            {
                deviceAlarmDay.AlarmCount = deviceAlarmDay.AlarmCount+1;
                deviceAlarmDayServices.UpdateEntity(deviceAlarmDay);
            }
            AddAlarmMonth(device);//增加月数据
        }
        /// <summary>
        /// 更新报警天数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void UpdateAlarmDay(DeviceInfo device,DeviceAlarmHistory deviceAlarm)
        {
            DeviceAlarmDay deviceAlarmDay = new DeviceAlarmDay();
            TimeSpan timeSpan = deviceAlarm.ConfirmTime - deviceAlarm.StartTime;
            deviceAlarmDay = deviceAlarmDayServices.QueryableToEntity(x => x.Date == DateTime.Now.Date && x.DeviceId == device.DeviceId);
            if (deviceAlarmDay == null)
            {
                deviceAlarmDay = new DeviceAlarmDay();
                deviceAlarmDay.AlarmCount = 1;
                deviceAlarmDay.DurationMin = timeSpan.TotalMinutes;
                deviceAlarmDay.ConfirmTime = timeSpan.TotalMinutes;
                deviceAlarmDay.Date = DateTime.Now.Date;
                deviceAlarmDay.DeviceId = device.DeviceId;
                deviceAlarmDay.DeviceName = device.DeviceName;
                deviceAlarmDayServices.InsertBigIdentity(deviceAlarmDay);
            }
            else
            {
                deviceAlarmDay.DurationMin += timeSpan.TotalMinutes;
                deviceAlarmDay.ConfirmTime += timeSpan.TotalMinutes;
                deviceAlarmDayServices.UpdateEntity(deviceAlarmDay);
            }
            UpdateAlarmMonth(device, deviceAlarm);//更新月数据
        }
        /// <summary>
        /// 增加报警月数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void AddAlarmMonth(DeviceInfo device)
        {
            DateTime dtNow = DateTime.Now;
            DeviceAlarmMonth deviceAlarmMonth = new DeviceAlarmMonth();
            int year = dtNow.Year;
            int month = dtNow.Month;

            deviceAlarmMonth = deviceAlarmMonthServices.QueryableToEntity(x =>x.Year== year &&
                                                                              x.Month== month && 
                                                                              x.Date == dtNow.Date &&
                                                                              x.DeviceId == device.DeviceId);
            if (deviceAlarmMonth == null)
            {
                deviceAlarmMonth = new DeviceAlarmMonth();
                deviceAlarmMonth.AlarmCount = 1;
                deviceAlarmMonth.Year = dtNow.Year;
                deviceAlarmMonth.Month = dtNow.Month;
                deviceAlarmMonth.Date = DateTime.Now.Date;
                deviceAlarmMonth.DeviceId = device.DeviceId;
                deviceAlarmMonthServices.InsertBigIdentity(deviceAlarmMonth);
            }
            else
            {
                deviceAlarmMonth.AlarmCount += 1;
                deviceAlarmMonthServices.UpdateEntity(deviceAlarmMonth);
            }
        }
        /// <summary>
        /// 更新报警月数据
        /// </summary>
        /// <param name="device"></param>
        /// <param name="clientInfo"></param>
        private void UpdateAlarmMonth(DeviceInfo device, DeviceAlarmHistory deviceAlarm)
        {
            DateTime dtNow = DateTime.Now;
            int year = dtNow.Year;
            int month = dtNow.Month;
            DeviceAlarmMonth deviceAlarmMonth = new DeviceAlarmMonth();
            TimeSpan timeSpan = deviceAlarm.ConfirmTime - deviceAlarm.StartTime;
            deviceAlarmMonth = deviceAlarmMonthServices.QueryableToEntity(x => x.Year == year &&
                                                                               x.Month == month &&
                                                                               x.Date == dtNow.Date &&
                                                                               x.DeviceId == device.DeviceId);
            if (deviceAlarmMonth == null)
            {
                deviceAlarmMonth = new DeviceAlarmMonth();
                deviceAlarmMonth.AlarmCount = 1;
                deviceAlarmMonth.DurationMin = timeSpan.TotalMinutes;
                deviceAlarmMonth.ConfirmTime = timeSpan.TotalMinutes;
                deviceAlarmMonth.Date = DateTime.Now.Date;
                deviceAlarmMonth.DeviceId = device.DeviceId;
                deviceAlarmMonthServices.InsertBigIdentity(deviceAlarmMonth);
            }
            else
            {
                deviceAlarmMonth.DurationMin += timeSpan.TotalMinutes;
                deviceAlarmMonth.ConfirmTime += timeSpan.TotalMinutes;
                deviceAlarmMonthServices.UpdateEntity(deviceAlarmMonth);
            }
        }

        /// <summary>
        /// 报警明细
        /// </summary>
        /// <param name="DeviceId"></param>
        /// <param name="HistoryId"></param>
        /// <param name="deviceData"></param>
        public void AddAlarmDetail(int DeviceId, int HistoryId, AliIotDeviceData<ECU1151Info> deviceData)
        {
            //AddAlarmDetail(DeviceId, HistoryId, "A相电压", deviceData.items.Ua.value);
            //AddAlarmDetail(DeviceId, HistoryId, "B相电压", deviceData.items.Ub.value);
            //AddAlarmDetail(DeviceId, HistoryId, "C相电压", deviceData.items.Uc.value);
            //AddAlarmDetail(DeviceId, HistoryId, "A相电流", deviceData.items.Ia.value);
            //AddAlarmDetail(DeviceId, HistoryId, "B相电流", deviceData.items.Ib.value);
            //AddAlarmDetail(DeviceId, HistoryId, "C相电流", deviceData.items.Ic.value);
            //AddAlarmDetail(DeviceId, HistoryId, "温度通道1", deviceData.items.T1.value.T);
            //AddAlarmDetail(DeviceId, HistoryId, "温度通道2", deviceData.items.T2.value.T);
            //AddAlarmDetail(DeviceId, HistoryId, "温度通道3", deviceData.items.T3.value.T);
            //AddAlarmDetail(DeviceId, HistoryId, "温度通道4", deviceData.items.T4.value.T);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DeviceId"></param>
        /// <param name="HistoryId"></param>
        /// <param name="ParameterName"></param>
        /// <param name="ParameterValue"></param>
        private void AddAlarmDetail(int DeviceId, int HistoryId, string ParameterName, object ParameterValue)
        {
            DeviceAlarmDetail deviceAlarmDetail = new DeviceAlarmDetail();
            deviceAlarmDetail.HistoryId = HistoryId;
            deviceAlarmDetail.ParameterName = ParameterName;
            deviceAlarmDetail.ParameterValue = ParameterValue.ToString();
            deviceAlarmDetail.CreateDate = DateTime.Now;
            deviceAlarmDetailServices.Insert(deviceAlarmDetail);
        }
    }
}
